package com.mall.manager.abilitystatic.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mall.common.attributes.CommonAttributes;
import com.mall.common.entity.AbilityStaticstic;
import com.mall.manager.abilitystatic.service.AbilityService;

@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/manager")
@Controller
public class AbilityController {

	@Resource
	AbilityService abilityService;
	

	/**
	 * 查询八大能力平均分
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/abilityMap")
	public String findAbilityStatics(HttpServletRequest request,ModelMap modelMap){
		List list = abilityService.findAbility(request);
		Map<String,String> mp = new HashMap<String,String>();
		Map<String,String> province = CommonAttributes.getMap();
		if(list!=null&&list.size()>0){
			JSONArray ar = JSONArray.fromObject(list);
			for(int i=0;i<ar.size();i++){
				Map<String,Object> map = JSONObject.fromObject(ar.get(i));
				AbilityStaticstic abs = new AbilityStaticstic();
				String areaName = MapUtils.getString(map, "area_name");
				abs.setId(MapUtils.getInteger(map, "id"));
				abs.setAreaId(MapUtils.getString(map, "area_id"));
				abs.setAreaName(areaName);
				abs.setArtAvg(MapUtils.getDouble(map, "art_avg"));
				abs.setCognitionAvg(MapUtils.getDouble(map, "cognition_avg"));
				abs.setLanguageAvg(MapUtils.getDouble(map, "language_avg"));
				abs.setMathAvg(MapUtils.getDouble(map, "math_avg"));
				abs.setPerfectAvg(MapUtils.getDouble(map, "perfect_avg"));
				abs.setSelfAvg(MapUtils.getDouble(map, "self_avg"));
				abs.setSocialAvg(MapUtils.getDouble(map, "social_avg"));
				abs.setSportAvg(MapUtils.getDouble(map, "sport_avg"));
				abs.setTotalAvg(MapUtils.getDouble(map, "total_avg"));
				modelMap.addAttribute(areaName, abs);
				//保存存在的省
				mp.put(areaName, areaName);
			}
		}
		//存在省份数据为0的情况
		if(mp.size()>0&&mp.size()!=34){
			for(String key:province.keySet()){
				if(!mp.containsKey(key)){
					AbilityStaticstic abs = new AbilityStaticstic();
					abs.setId(-1);
					abs.setAreaId("-1");
					abs.setAreaName("无");
					abs.setArtAvg(0d);
					abs.setCognitionAvg(0d);
					abs.setLanguageAvg(0d);
					abs.setMathAvg(0d);
					abs.setPerfectAvg(0d);
					abs.setSelfAvg(0d);
					abs.setSocialAvg(0d);
					abs.setSportAvg(0d);
					abs.setTotalAvg(0d);
					modelMap.addAttribute(key, abs);
				}
			}
		}
		return "abilitystatics/abilitystatic_map";
	}
}






